package com.sky.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.ISetmealService;
import com.sky.service.SetmealDishesService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.Du
 * @Date 2025/5/21 2025/5/21
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesService setmealDishesService;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //设置创建时间、修改时间、创建人、修改人
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        this.save(setmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null) {
            for (SetmealDish meal : setmealDishes) {
                meal.setSetmealId(setmeal.getId());
            }
        }
        setmealMapper.banchInsert(setmealDishes);
    }

    /**
     * 分页查询套餐列表
     *
     * @return
     */
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO dto) {
        // 初始化分页构造器
        IPage<Setmeal> defaultPage = new Page<>(dto.getPage(), dto.getPageSize());
        //  设置查询条件
        IPage<Setmeal> page = this.lambdaQuery()
                .like(dto.getName() != null, Setmeal::getName, dto.getName())
                .eq(dto.getCategoryId() != null, Setmeal::getCategoryId, dto.getCategoryId())
                .eq(dto.getStatus() != null, Setmeal::getStatus, dto.getStatus())
                .page(defaultPage);
        //使用数据对象调用mybatisPlus的分页查询方法selectPage(分页构造器,查询条件wrapper对象)
        long total = page.getTotal();
        List<Setmeal> records = page.getRecords();
        return new PageResult(total, records);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Integer id) {
        Setmeal setmeal = this.lambdaQuery().eq(Setmeal::getId, id).one();
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishesService.lambdaQuery()
                .eq(SetmealDish::getSetmealId, id).list();
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param dto
     */
    @Override
    public void updateSetmeal(SetmealDTO dto) {
        //保存更新的套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
               this.updateById(setmeal);
        //设置修改时间、修改人
//        setmeal.setUpdateTime(LocalDateTime.now());
//        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //删除旧套餐菜品
        setmealDishesService.lambdaUpdate()
                .eq(SetmealDish::getSetmealId, setmeal.getId())
                .remove();
        List<SetmealDish> setmealDishesNew = dto.getSetmealDishes();
        if (setmealDishesNew != null) {
            for (SetmealDish meal : setmealDishesNew) {
                meal.setSetmealId(setmeal.getId());
            }
        }
        setmealDishesService.saveOrUpdateBatch(setmealDishesNew);
    }
}
